<?php
/** 
 * Created by RisingSun. 
 * User: MarkSpace 
 * Date: 20210822
 * Time: 11:05 
 * 合同管理
 */
namespace app\api\controller;

use  Mscore\Core\Controller;
use  app\api\model\Contract as ContractModel;
use  app\api\model\Common as CommonModel;
use  Mscore\Core\Lib\ArrayMethod;
use  Mscore\Core\Lib\Upload;
use  app\api\model\Login as LoginModel;

class ContractController extends Controller{

	private $model;
    private $login;
    private $error;
    private $success; 
    /**
     * 初始化自动连接方法
     * @param array $sysmod 
     * @param array $where 
    */
    public function __construct()  
    {    
        $this->model = new ContractModel;
        $this->login = (new LoginModel)->isAuth();
        $this->header =get_all_headers();
        $this->header['language']=isset($this->header['language'])&&!empty($this->header['language'])?$this->header['language']:config('langs.default');
        Lang::init($this->header['language']);
        $this->success=Lang::get('Success');
        $this->error=Lang::get('Error');
    }   

    /**
     * 协议合同模板列表
     * @param array $cate 
     * @param array $where 
     */ 
    public function cate(){  
        switch (true) {
            case !empty(post('id')):
                $where = 'a.id='.post('id');
                $datalist = $this->model->cate($where);
                if(!empty($datalist['data'][0]['titles'])){
                    $datalist['data'][0]['title']=isset(json_decode($datalist['data'][0]['titles'],true)[$this->header['language']])?json_decode($datalist['data'][0]['titles'],true)[$this->header['language']]:$datalist['data'][0]['title'];
                    unset($datalist['data'][0]['titles']);
                }
                if(!empty($datalist['data'][0]['introduction'])){
                    $datalist['data'][0]['introduction']=isset(json_decode($datalist['data'][0]['introduction'],true)[$this->header['language']])?json_decode($datalist['data'][0]['introduction'],true)[$this->header['language']]:'';
                }
                if(!empty($datalist['data'][0]['content'])){
                    $datalist['data'][0]['content']=isset(json_decode($datalist['data'][0]['content'],true)[$this->header['language']])?htmlspecialchars_decode(json_decode($datalist['data'][0]['content'],true)[$this->header['language']]):'';
                }
                $this->success['data']=$datalist['data'];
                $this->success['count']=$datalist['total'];
                $this->echojson($this->success);
                break;
            default:
                $page = post('page',1);
                $limit = post('limit',10);
                $where = ' a.language = "'.$this->header['language'].'" ';
                if(post('title')){
                    $where .= ' and a.title like "%'.post('title').'%"';
                }
                if(post('acode')){
                    $where .= ' and a.acode like "%'.post('acode').'%"';
                }
                $file='a.title, a.titles, a.acode, a.introduction, a.imgurl, a.is_type, a.status, a.create_time';
                $datalist = $this->model->cate($where,$page,$limit,$file);
                foreach ($datalist['data'] as $key => $value) {
                    if(!empty($value['titles'])){
                        $datalist['data'][$key]['title']=isset(json_decode($value['titles'],true)[$this->header['language']])?json_decode($value['titles'],true)[$this->header['language']]:$value['title'];
                    }
                    if(!empty($value['introduction'])){
                        $datalist['data'][$key]['introduction']=isset(json_decode($value['introduction'],true)[$this->header['language']])?json_decode($value['introduction'],true)[$this->header['language']]:'';  
                    }
                    unset($datalist['data'][$key]['titles']);
                }
                $this->success['data']=$datalist['data'];
                $this->success['count']=$datalist['total'];
                $this->echojson($this->success);
                break;
        }
    }
  
}
